package com.gzsxy.sso.resources.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Collection;

/**
 * @author xiaolong
 * @version 1.0
 * @description: web请求返回的json对象
 * @date 2021/11/8 11:41
 */
public class Result extends JSONObject {

    //请求返回对象属性-是否成功
    public final static String PROPERTY_SUCCESS = "success";

    //请求返回对象属性-返回消息
    public final static String PROPERTY_MESSAGE = "msg";

    //请求返回数据属性-代码
    public final static String PROPERTY_CODE = "code";

    //请求返回数据属性-数据 （JsonObject描述 ，适用于单个实体或者其他类型返回）
    public final static String PROPERTY_DATA = "Data";

    //请求返回数据属性-结果数量
    public final static String PROPERTY_TOTAL = "Total";

    //请求返回数据属性-结果集（JSONArray描述，适用结果集返回）
    public final static String PROPERTY_RESULT = "Rows";

    //是否设置过data属性的值，在调用simplifyf方法后保留data的key属性
    public boolean iskeepDataProperty = false;

    public Result() {
        put(PROPERTY_SUCCESS,true);
        put(PROPERTY_MESSAGE,"");
        put(PROPERTY_DATA,new JSONObject());
        put(PROPERTY_RESULT,new JSONObject());
        put(PROPERTY_TOTAL,0);
    }


    public static Result success(){
        return new Result();
    }


    public static Result success(Object obj,String msg){
        Result r = new Result();
        r.put(PROPERTY_DATA,obj);
        r.put(PROPERTY_MESSAGE,msg);
        return r;
    }

    public static Result success(String msg){
        Result r = new Result();
        r.put(PROPERTY_MESSAGE,msg);
        return r;
    }

    public static Result success(Collection<?> result, String msg,long total){
        Result r = new Result();
        r.put(PROPERTY_RESULT,result);
        r.put(PROPERTY_MESSAGE,msg);
        r.put(PROPERTY_TOTAL,total);
        return r;
    }


    public static Result success(JSONObject obj, String msg, long total){
        Result r = new Result();
        r.put(PROPERTY_DATA,obj);
        r.put(PROPERTY_MESSAGE,msg);
        r.put(PROPERTY_TOTAL,total);
        return r;
    }


    public static Result error(String msg){
        Result r = new Result();
        r.put(PROPERTY_SUCCESS,false);
        r.put(PROPERTY_MESSAGE,msg);
        r.put(PROPERTY_CODE,"-1");
        return r;
    }

    public static Result error(String msg,String code){
        Result r = new Result();
        r.put(PROPERTY_SUCCESS,false);
        r.put(PROPERTY_MESSAGE,msg);
        r.put(PROPERTY_CODE,code);
        return r;
    }

    public static Result error(){
        Result r = new Result();
        r.put(PROPERTY_SUCCESS,false);
        r.put(PROPERTY_CODE,"-1");
        return r;
    }


    public  Result putData(Object value){
        super.put(PROPERTY_DATA,value);
        iskeepDataProperty = true;
        return this;
    }

    @Override
    public Object put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    /**
     * @description: 简化JSON输出内容
     * @param
     * @return
     */
    public Result simplify(){
        try {
            JSONObject dataJson = getJSONObject(PROPERTY_DATA);
            if ((dataJson == null || dataJson.isEmpty()) && !iskeepDataProperty){
                super.remove(PROPERTY_DATA);
            }
        } catch (Exception e) {
        }
        try {
            JSONArray dataJson = getJSONArray(PROPERTY_RESULT);
            if (dataJson == null || dataJson.isEmpty()){
                super.remove(PROPERTY_RESULT);
            }
        } catch (Exception e) {
        }

        try {
            Integer totalInt = getInteger(PROPERTY_TOTAL);
            if (totalInt == null || totalInt == 0){
                super.remove(PROPERTY_TOTAL);
            }
        } catch (Exception e) {
        }

        return this;

    }
}
